package com.penghaisoft.microwms.base.web.data.controller;


import java.util.List;

import com.penghaisoft.microwms.base.web.data.service.IMdContractService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseFactoryUser;
import com.penghaisoft.microwms.common.base.model.BaseFactoryUserKey;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdContract;
import com.penghaisoft.microwms.common.data.model.MdContractAfterSalerRel;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * zhangx
 * 合同管理
 */
@RestController
@RequestMapping(value = "/data/contract")
@Slf4j
public class MdContractController extends BaseController {

	@Autowired
	private IMdContractService mdContractService;

	@PostMapping("c")
	public TransResult create(MdContract mdContract) {
		TransResult result = new TransResult();
		mdContract.setCreateBy(getLoginName());
		Resp resp =  mdContractService.create(mdContract);
		result.setCode(resp.getCode());
		result.setMsg(resp.getMsg());
		return result;
	}

	@PostMapping("d")
	public TransResult delete(MdContract mdContract) {
		mdContract.setLastModifiedBy(getLoginName());
		Resp resp =  mdContractService.delete(mdContract);
		return fromResp(resp);
	}

	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
							@RequestParam(name = "rows", defaultValue = "10") int rows, MdContract mdContract) {
		Pager<MdContract> result = mdContractService.findListByCondition(page,rows,mdContract);
		return success(result);
	}

	@GetMapping("r/{id}")
	public TransResult queryById(@PathVariable String id) {
		MdContract  mdContract = mdContractService.findById(id);
		return success(mdContract);
	}

	@PostMapping("u")
	public TransResult update(MdContract mdContract) {
		mdContract.setLastModifiedBy(getLoginName());
		mdContract.preUpdate();
		Resp resp =  mdContractService.update(mdContract);
		return fromResp(resp);
	}
	
	/** 
	 * @Title queryContract
	 * @Description 获取合同号
	 * @author luoteng
	 * @date 2018年5月28日:上午10:08:45
	 * @param dicType
	 * @return TransResult
	 */
	@GetMapping("get")
	public TransResult queryContract(MdContract mdContract) {
		mdContract.setActiveFlag("1");
		List<MdContract> list = mdContractService.queryList(mdContract);
		return success(list);
	}
	
	/** 
	 * @Title queryContractList
	 * @Description 手持合同号下拉框
	 * @author luoteng
	 * @date 2018年6月6日:上午9:39:53
	 * @param mdContract
	 * @return HandResp
	 */
	@GetMapping("hand/select/get")
	public HandResp queryContractList(MdContract mdContract) {
		HandResp rtnResp = new HandResp();
		mdContract.setActiveFlag("1");
		List<MdContract> list = mdContractService.queryList(mdContract);
		rtnResp.setCode(Constant.SUCCESS);
		rtnResp.setList(list);
		return rtnResp;
	}
	
	/**  
	 * @Title: queryUserByContract
	 * @Description:   查询该合同号下的售后经理
	 * @author Lengxf  
	 * @date 2019年3月20日    
	 */  
	@GetMapping("/{contractNo}/user")
	public TransResult queryUserByContract(@PathVariable String contractNo){
		MdContractAfterSalerRel entity = new MdContractAfterSalerRel();
		entity.setContractNo(contractNo);
		List<MdContractAfterSalerRel> list = mdContractService.queryUserByContract(entity);
		return success(list);
	}
	
	/**  
	 * @Title: updateContractAfterSaler
	 * @Description:   修改该合同号下的售后经理
	 * @author Lengxf  
	 * @date 2019年3月20日    
	 */  
	@PostMapping("/updateContractAfterSaler")
	public TransResult updateContractAfterSaler(@RequestBody List<MdContractAfterSalerRel> mdContractAfterSalerRel){
		TransResult result = new TransResult();
		String userName =  getUserName();
		if(!userName.equals(mdContractAfterSalerRel.get(0).getUserDefined2())){
			result.setCode(Constant.FAIL);
			result.setMsg("只能修改本人销售合同下的售后经理");
			return result;
		}else{
			Resp resp = mdContractService.updateContractAfterSaler(mdContractAfterSalerRel);
			return fromResp(resp);
		}
		
	}
	
	/**  
	 * @Title: checkIsMyself
	 * @Description:   校验是否为本人操作
	 * @author Lengxf  
	 * @date 2019年3月20日    
	 */  
	@PostMapping("/checkIsMyself/{userName}")
	public TransResult checkIsMyself(@PathVariable String userName){
		TransResult result = new TransResult();
		String thisUserName =  getUserName();
		if(!thisUserName.equals(userName)){
			result.setCode(Constant.FAIL);
			result.setMsg("只能修改本人销售合同下的售后经理");
			return result;
		}else{
			result.setCode(Constant.SUCCESS);
			return result;
		}
	}
	
	
}
